package com.my.demo.config;


import com.baomidou.mybatisplus.annotation.TableField;
import com.my.demo.domain.A;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResultMapUtil {

    public static void production(Class c){
        Field[] declaredFields = c.getDeclaredFields();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<resultMap id=\"");
        stringBuffer.append(c.getSimpleName());
        stringBuffer.append("\" type=\"");
        stringBuffer.append(c.getCanonicalName());
        stringBuffer.append("\">\n");
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(TableField.class)){
                TableField annotation = declaredField.getAnnotation(TableField.class);
                if (!annotation.exist()){
                    continue;
                }
            }
            if ("id".equals(declaredField.getName())){
                stringBuffer.append("<id column=\"");
            } else if ("serialVersionUID".equals(declaredField.getName())) {
                continue;
            } else {
                stringBuffer.append("<result column=\"");
            }

            stringBuffer.append(humpToLine(declaredField.getName()));
            stringBuffer.append("\" property=\"");
            stringBuffer.append(declaredField.getName());
            stringBuffer.append("\"/>\n");
        }
        stringBuffer.append("</resultMap>");
        System.out.println(stringBuffer);
    }


    public static String humpToLine(String str) {
        Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) {
        production(A.class);
    }


}

